﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Meals
{
    public class UserMealFavs:IUserMealFavs
    {
        public void AddMealToUserFavs(int userId, int MealId, ref Model.NutriLeafDBEntities context)
        {
            Model.UserMealFav fav = new Model.UserMealFav();
            fav.UserID = userId;
            fav.MealID = MealId;
            context.UserMealFavs.AddObject(fav);
            context.SaveChanges();
        }

        public void RemoveMealFromUserFavs(int userId, int MealId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.UserMealFavs.Where(p => p.MealID == MealId && p.UserID == userId).First());
            context.SaveChanges();
        }

        public void RemoveMealFromUserFavs(int userMealFavsId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.UserMealFavs.Where(p => p.UserMealFavID == userMealFavsId).First());
            context.SaveChanges();
        }

        public List<Model.Meal> GetAllUserFavsMeal(int userId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.UserMealFav> fav = context.UserMealFavs.Where(p => p.UserID == userId).ToList();
            List<Model.Meal> Meal = new List<Model.Meal>();
            foreach (Model.UserMealFav f in fav) {
                Meal.Add(f.Meal);
                
            }
            return Meal; 
        }
    }
}